using System;
using System.Collections.Generic;
using System.Text;
using DataAccess;
using NHibernate;
using NHibernate.Expression;

namespace Task
{
    public class CommentTask
    {




        public void AddComment(int userId, int artId, string content)
        {
            NHibernateDao<comment, int> factory = new NHibernateDao<comment, int>();
            comment newComment = new comment();

            newComment.date = DateTime.Now;
            newComment.user_id = userId;
            newComment.art_id = artId;
            newComment.contents = content;
        }

        public void FindCommentByArtId(int artId)
        {
            NHibernateDao<comment, int> factory = new NHibernateDao<comment, int>();
            List<comment> comments = 
                factory.GetByCriteria(NHibernate.Expression.Expression.Eq("art_id", artId));
            comments.Sort
            ( 
            delegate(comment c1, comment c2) 
                {
                    if (   ((TimeSpan)(c1.date - c2.date)).TotalMilliseconds > 0)
                        return -1;
                    return 1;
                }
            );


        }

        public IList<comment> FindLastArtCommentsForUser(int userId, int number)
        {

            NHibernateDao<DataAccess.comment, int> factory = new NHibernateDao<DataAccess.comment, int>();
            ISession session = NHibernateSessionManager.Instance.GetSession();
            //System.Collections.IList result = session.CreateQuery("from comment as c left join fetch c.art_id a left join fetch c.user_id where a.user_id = :userId order by c.date desc")
            //    .SetString("userId", userId.ToString()).List();

            IList<comment> result = session.CreateQuery("from comment as c left join fetch c.art as a left join fetch a.gallery as g left join fetch a.user where c.art.user.id = :userId order by c.date desc").SetString("userId", userId.ToString()).SetMaxResults(number).List<comment>();
            
            return result;

        }



    }
}
